import java.util.AbstractCollection;
import java.util.NoSuchElementException;



public class ArrayStack extends AbstractCollection<Integer>{
        private Integer [] arr={};
        private int count=0;
        
        public boolean empty(){
                if (size()==0) return true;
                else return false;
        }
        
        public int peek(){
                if(!empty()){
                        return arr[size()-1];                   
                }
                else throw new NoSuchElementException("No more elements");
                
        }
        
        public int pop(){
                if(!empty()){
                        count--;
                        int a=arr[count];
                        arr[count]=0;
                        return a;
                                                
                }
                else throw new NoSuchElementException("No more elements");
        }
        
        public void push(Integer a){
                if(count==arr.length)
                {
                        count++;
                        Integer[] arrcopy=new Integer[size()*2];
                        System.arraycopy(arr, 0, arrcopy, 0, (size()-1));
                        arrcopy[size()-1]=a;
                        arr=arrcopy;
                }
                else
                {
                        arr[count]=a;
                        count++;                        
                }
        }
        
        public void clear()
        {
                while(!empty())
                        pop();
        }
        
        public int search (Integer a){
                int i=size();
                while (i!=0&&!arr[i-1].equals(a)){  
                        i--;
                }
                return ((i==0)?-1:size()-i);
        }
        
        public class Iterator implements java.util.Iterator<Integer>{

                private Integer count=null;
                public Iterator(){
                        this.count=size();
                        
                }
                @Override
                public boolean hasNext() {
                        return (this.count != 0)?true:false;
                }

                @Override
                public Integer next() {
                        if(hasNext()){
                                this.count--;
                                return arr[count-1];                            
                        }                       
                        else throw new NoSuchElementException("No more elements");
                        
                }

                @Override
                public void remove() {
                        throw new UnsupportedOperationException("This operation isn't supported in this collection");                   
                }
                
        }

        @Override
        public int size() {
                return count;
        }

        @Override
        public java.util.Iterator<Integer> iterator() {
                Iterator it= new Iterator();
                return it;
        }
        
        public String toString() {
        StringBuilder s = new StringBuilder();
        s.append("[");
        for (int i = 0; i < size(); i++) {
            s.append(arr[i]);
            if (size() - i > 1) {
                s.append(", ");
            }
        }
        s.append("]");
        return s.toString();
    }

}